<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card class="box-card">
      <el-row :gutter=20 class="search-box">
        <el-col :span="7">
          <el-input placeholder="请输入内容" v-model="paramObj.query" clearable>
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="3">
          <el-button type="primary" @click="addPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品表格区域 -->
      <el-table :data="goodsList" border style="width: 100%">
        <!-- 索引列 -->
        <el-table-column type="index" label="#">
        </el-table-column>
        <el-table-column prop="goods_name" label="商品名称">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteState(scope.row.goods_id)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页视图区域 -->
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="paramObj.pagenum" :page-sizes="[10, 30, 50]" :page-size="paramObj.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
      <!-- 编辑对话框 -->

    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      goodsList: [],
      // 获取商品列表的接口参数项
      paramObj: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 总页数
      total: 0,
      // 删除对话框的状态
      editDialog: false,

    }

  },
  methods: {
    // 获取商品列表
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', { params: this.paramObj });
      // console.log(res)
      if (res.meta.status !== 200) return this.$msg.error("获取商品列表失败");
      this.goodsList = res.data.goods;
      this.total = res.data.total;
    },
    // 监听页面大小变化
    handleSizeChange(newSize) {
      this.paramObj.pagesize = newSize;
      this.getGoodsList();
    },
    // 监听页码变化
    handleCurrentChange(newPage) {
      this.paramObj.pagenum = newPage;
      this.getGoodsList();
    },
    // 删除操作
    deleteState(id) {
      console.log(id)
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteData(id);
        this.$msg({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$msg({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 删除商品数据
    async deleteData(id) {
      const { data: res } = await this.$http.delete(`goods/${id}`);
      console.log(res)
      if (res.meta.status !== 200) return this.$msg.error("删除失败");
      this.$msg.success("删除成功");
      this.getGoodsList();

    },
    // 跳转到添加商品页面
    addPage() {
      this.$router.push('/goods/add')
    }


  },
  created() {
    this.getGoodsList();
  }

}
</script>

<style lang="less" scoped>
</style>